<template>
  <header class="header">
    <h1>todos</h1>
    <input v-model="isAll" id="toggle-all" class="toggle-all" type="checkbox" >
    <label for="toggle-all"></label>
    <input
      class="new-todo"
      placeholder="输入任务名称-回车确认"
      autofocus
      @keyup.enter="onKeyup"
      v-model.trim="task"
    />
  </header>
</template>

<script>
export default {
  props:['arr'],
  data(){
    return{
      task:'' // 添加输入框的值
    }
  },
  methods: {
    // 添加
    onKeyup(){
      if(this.task==='') return alert('请输入任务名称')
      this.$emit('keyupAdd',this.task)
      this.task=''
    }
  },
  computed: {
    // 根据数据计算是否全选
    isAll:{
      get(){
        return this.arr.length && this.arr.every(it=>it.isDone)
      },
      set(checked){
        this.arr.forEach(it=>it.isDone=checked)
      }
    }
  },
}
</script>
